package com.caoustc.okhttplib.okhttp.utils

import android.text.TextUtils
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.caoustc.okhttplib.okhttp.platform.Stat
import java.lang.reflect.Type

/**
 * Created by cz on 2018/3/13.
 */
object ParseUtils {
    private const val TAG = "ParseUtils"

    /**
     * 解析复杂数据类型，循环嵌套解析.
     * parse json one or two floor
     *
     * @param jsonObject
     * @param parseKey
     * @param type
     * @return object
     */
    @kotlin.jvm.JvmStatic
    fun parseJson(
        jsonObject: JSONObject?,
        parseKey: String?,
        type: Type?,
        dataClass: Class<*>?
    ): Any? {
//        KLog.i(TAG,"parseJson # dataClass "+dataClass);
        var obj: Any? = null
        if (jsonObject == null) {
            return null
        }
        if (TextUtils.isEmpty(parseKey)) {
            obj = JSON.parseObject(jsonObject.toJSONString(), type)
        } else { //解析data类型数据结构.
            var dataObject: JSONObject? = null
            try {
                //此处兼容后台返回null，造成返回null和kolin的callback非空不兼容导致的无法巡店问题.
                dataObject = jsonObject.getJSONObject(parseKey)
            } catch (e: Exception) {
                val dataArray = jsonObject.getJSONArray(parseKey)
                //使用Gson解析jsonArray。
//                KLog.i(TAG,"parse array start!~dataClass:"+dataClass+" data: "+dataArray);
                obj = JSONArray.parseArray(dataArray.toJSONString(), dataClass)
                //                KLog.i(TAG,"parse array end!~obj:"+obj.getClass());
                return obj
            }
            if (dataObject == null) {
                //obj = JSON.parseObject(jsonObject.toJSONString(), type);
                return null
            }
            obj = JSON.parseObject(jsonObject.getJSONObject(parseKey).toJSONString(), type)
        }
        return obj
    }

    /**
     * parse result
     *
     * @param jsonObject
     * @param key
     * @return
     */
    @kotlin.jvm.JvmStatic
    fun parseResult(jsonObject: JSONObject, key: String?): String? {
        var resultCode: String? = null
        if (jsonObject.containsKey(key)) {
            resultCode = jsonObject.getString(key)
        }
        return resultCode
    }

    @kotlin.jvm.JvmStatic
    fun parseTotal(jsonObject: JSONObject, key: String?): Int {
        var total = 0
        if (jsonObject.containsKey(key)) {
            total = jsonObject.getInteger(key)
        }
        return total
    }

    @kotlin.jvm.JvmStatic
    fun parseBoolean(jsonObject: JSONObject, key: String?): Boolean? {
        var total: Boolean? = null
        if (jsonObject.containsKey(key)) {
            total = jsonObject.getBoolean(key)
        }
        return total
    }

    @kotlin.jvm.JvmStatic
    fun parseStat(jsonObject: JSONObject): Stat? {
        var stat: Stat? = null
        if (jsonObject.containsKey("stat")) {
            val dataObject = jsonObject.getJSONObject("stat")
            stat = JSON.parseObject(dataObject.toJSONString(), Stat::class.java)
        }
        return stat
    }
}